package genetic.q2;

import genetic.Genotype;
import genetic.TreeNode;
import genetic.q2.functions.RobyFunction;
import genetic.q2.functions.RobyTerminal;

public class RobyGenotype extends Genotype {

	double score;
	double max;

	public RobyGenotype(TreeNode tree) {
		super(tree);
	}

	@Override
	public Genotype clone() {
		return new RobyGenotype(tree.clone());
	}

	public void moveOnce(board b){
		int[] ans;
		if (tree.isFunction()){
			ans = ((RobyFunction)tree).move(b);
		} else {
			ans = ((RobyTerminal)tree).move(b);
		}

		addScore(b.moved(ans[0]));
	}

	public void move(board b){
		max = max + b.startBoard();
		for (int i = 0; i < 200; i++) {
			moveOnce(b);
		}		
	}
	
	public void resetScore(){
		score = 0;
		max = 0;
	}

	public void addScore(double s){
		score = score + s;
	}

	public double getScore() {
		if(score < 0)
			return score/100000;
		else
			return (score)/(max);
	}
	
	@Override
	public String toString() {
		if(score < 0)
			return score/100000  + " - " + tree.toString();
		else
			return (score)/(max) + " - " + tree.toString();
	}
}
